//
//  LendViewController.m
//  ZYZB
//
//  Created by ENERGY on 2018/6/11.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "OldLendViewController.h"
#import "PlatinumViewController.h"
#import "InterestViewController.h"
#import "SiftViewController.h"

#define sliderLabelW 45/WIDTH_6_SCALE
#define btnW (SCREEN_WIDTH - 30/WIDTH_6_SCALE)/3
#define btnX (btnW - sliderLabelW)/2
@interface OldLendViewController ()<UIScrollViewDelegate>{
    PlatinumViewController * platinumVC;
    InterestViewController * interestVC;
    SiftViewController * siftVC;
    UIScrollView * mainScrollView;
    UIView * navView;
    UILabel * sliderLabel;
    UIButton * platinumBtn;
    UIButton * interestBtn;
    UIButton * siftBtn;
}
@end

@implementation OldLendViewController(networking)

@end

@implementation OldLendViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setStatusBarBackgroundColor:[UIColor whiteColor]];
    self.title = @"出借";
}

- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self setMainSrollView];
    //设置默认
    [self sliderWithTag:self.currentIndex+1];
}

-(PlatinumViewController *)platinumVC{
    if (platinumVC == nil) {
        platinumVC = [[PlatinumViewController alloc] init];
        platinumVC.navigationController = self.navigationController;
    }return platinumVC;
}

- (InterestViewController *)interestVC{
    if (interestVC == nil) {
        interestVC = [[InterestViewController alloc] init];
        interestVC.navigationController = self.navigationController;
    }return interestVC;
}

- (SiftViewController *)siftVC{
    if (siftVC == nil) {
        siftVC = [[SiftViewController alloc] init];
        siftVC.navigationController = self.navigationController;
    }return siftVC;
}

#pragma mark
#pragma mark 初始化三个UIButton和一个滑动的silderLabel，三个btn放到一个UIView（navView）上面。
-(void)initUI{
    navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30/WIDTH_6_SCALE, 30/WIDTH_6_SCALE)];
    //福乐白金
    platinumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [platinumBtn setTitleColor:getColor(@"ff8819") forState:UIControlStateSelected];
    [platinumBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    platinumBtn.frame = CGRectMake(0, 0, btnW, 20/WIDTH_6_SCALE);
    platinumBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [platinumBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [platinumBtn setTitle:@"福乐白金" forState:UIControlStateNormal];
    platinumBtn.tag = 1;
    platinumBtn.selected = YES;
    [navView addSubview:platinumBtn];
    //福乐月息
    interestBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    interestBtn.frame = CGRectMake(platinumBtn.right, platinumBtn.top, platinumBtn.width, platinumBtn.height);
    [interestBtn setTitleColor:getColor(@"ff8819") forState:UIControlStateSelected];
    [interestBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    interestBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [interestBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [interestBtn setTitle:@"福乐月息" forState:UIControlStateNormal];
    interestBtn.tag = 2;
    [navView addSubview:interestBtn];
    //福乐精选
    siftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    siftBtn.frame = CGRectMake(interestBtn.right, platinumBtn.top, platinumBtn.width, platinumBtn.height);
    [siftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [siftBtn setTitleColor:getColor(@"ff8819") forState:UIControlStateSelected];
    siftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    [siftBtn addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
    [siftBtn setTitle:@"福乐精选" forState:UIControlStateNormal];
    siftBtn.tag = 3;
    [navView addSubview:siftBtn];
    //下面滑动条
    sliderLabel = [[UILabel alloc]initWithFrame:CGRectMake(btnX, navView.bottom - 5/WIDTH_6_SCALE, 45/WIDTH_6_SCALE, 2/WIDTH_6_SCALE)];
    sliderLabel.backgroundColor = getColor(@"ff8819");
    [navView addSubview:sliderLabel];
    
    self.navigationItem.titleView = navView;
//    platinumBtn.backgroundColor = [UIColor greenColor];
//    interestBtn.backgroundColor = [UIColor yellowColor];
//    siftBtn.backgroundColor = [UIColor brownColor];
}
#pragma mark 初始化srollView
-(void)setMainSrollView{
    mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.frame.size.height)];
    mainScrollView.delegate = self;
    mainScrollView.backgroundColor = [UIColor whiteColor];
    mainScrollView.pagingEnabled = YES;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    mainScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:mainScrollView];

    NSArray *views = @[self.platinumVC.view,self.interestVC.view,self.siftVC.view];
    for (NSInteger i = 0; i<views.count; i++) {
        //把三个vc的view依次贴到mainScrollView上面
        UIView *pageView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH*i, 0, mainScrollView.frame.size.width, mainScrollView.frame.size.height-100)];
        [pageView addSubview:views[i]];
        [mainScrollView addSubview:pageView];
    }
    mainScrollView.contentSize = CGSizeMake(SCREEN_WIDTH*(views.count), 0);
    //滚动到_currentIndex对应的tab
    [mainScrollView setContentOffset:CGPointMake((mainScrollView.frame.size.width)*_currentIndex, 0) animated:YES];
}
- (void)sliderAction:(UIButton *)sender{
    if (self.currentIndex==sender.tag) {
        return;
    }
    [self sliderAnimationWithTag:sender.tag];
    [UIView animateWithDuration:0.3 animations:^{
        self->mainScrollView.contentOffset = CGPointMake(SCREEN_WIDTH*(sender.tag-1), 0);
    } completion:^(BOOL finished) {

    }];
}
- (void)sliderAnimationWithTag:(NSInteger)tag{
    self.currentIndex = tag;
    platinumBtn.selected = NO;
    interestBtn.selected = NO;
    siftBtn.selected = NO;
    UIButton *sender = [self buttonWithTag:tag];
    sender.selected = YES;
    //动画
    [UIView animateWithDuration:0.3 animations:^{
        self->sliderLabel.frame = CGRectMake((sender.width - sliderLabelW)/2 + sender.left, self->sliderLabel.frame.origin.y, self->sliderLabel.frame.size.width, self->sliderLabel.frame.size.height);

    } completion:^(BOOL finished) {
        self->platinumBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        self->interestBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        self->siftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        sender.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    }];
}

- (UIButton *)buttonWithTag:(NSInteger )tag{
    if (tag==1) {
        return platinumBtn;
    }else if (tag==2){
        return interestBtn;
    }else if (tag==3){
        return siftBtn;
    }else{
        return nil;
    }
}

-(void)sliderWithTag:(NSInteger)tag{
    self.currentIndex = tag;
    platinumBtn.selected = NO;
    interestBtn.selected = NO;
    siftBtn.selected = NO;
    UIButton *sender = [self buttonWithTag:tag];
    sender.selected = YES;
    //动画
    sliderLabel.frame = CGRectMake(btnX, sliderLabel.frame.origin.y, sliderLabel.frame.size.width, sliderLabel.frame.size.height);
    platinumBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    interestBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    siftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    sender.titleLabel.font = [UIFont boldSystemFontOfSize:14];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //实时计算当前位置,实现和titleView上的按钮的联动
    CGFloat contentOffSetX = scrollView.contentOffset.x;
//    CGFloat X = contentOffSetX * (3*SCREEN_WIDTH/3)/navView.width/3;
//    CGRect frame = sliderLabel.frame;
//    frame.origin.x = X + btnX;
//    sliderLabel.frame = frame;
    NSLog(@"%f", contentOffSetX);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat contentOffSetX = scrollView.contentOffset.x;
    int index_ = contentOffSetX/SCREEN_WIDTH;
    [self sliderWithTag:index_+1];
}
@end
